<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
   rubric_association ||= nil; editable ||= false; association_object = nil; edit_view ||= false
   rubric ||= nil; assessing ||= false; assessment ||= nil; has_assessments ||= false
   for_context ||= false
   context = @context
   context = context.context if context.is_a?(Group)
   show_grading = !@assignment || !%w{online_quiz not_graded}.include?(@assignment.submission_types)
   rubric_association = assessment.rubric_association if assessment
%>
<% cache(['rubric_render3',
         (rubric.try(:cache_key) || 'no_rubric'),
         (rubric.try(:cache_key) || context.asset_string),
         (rubric_association.try(:cache_key) || 'no_association'),
         (assessment.try(:cache_key) || 'no_assessment'),
         editable,
         edit_view,
         assessing,
         show_grading,
         @domain_root_account.feature_enabled?(:rubric_criterion_range)].join('/')) do %>
<div
  class="rubric_container rubric <%= "assessing" if assessing %> <%= "for_grading" if rubric_association.try(:use_for_grading) %>"
  id="<%= rubric ? "rubric_#{rubric.id}" : "default_rubric" %>"
  style="<%= hidden unless rubric %>"
  tabindex="0"
>
  <div class="screenreader-only"><h2><%= t 'rubric', "Rubric" %></h2></div>
  <div class="rubric_title">
    <div style="float: right; <%= hidden unless editable && edit_view %>" class="links displaying">
      <% hash = rubric_association ? {:rubric_association_id => rubric_association.id} : {} %>
      <a
        href="<%= context_url(context, :context_rubric_url, rubric ? rubric.id : "{{ id }}", hash) %>"
        class="<%= 'copy_edit' if rubric && !can_do(rubric, @current_user, :update) %> edit_rubric_link no-print no-hover"
        style="<%= hidden unless !rubric || can_do(rubric_association, @current_user, :update) %>"
        title="<%= t 'links.edit', "Edit Rubric" %>"
        aria-label="<%= t 'links.edit', "Edit Rubric" %>"
        role="button"
      >{{> icons/edit}}</a>
      <a
        href="<%= search_rubrics_url(:q => "") %>"
        class="find_rubric_link no-print no-hover"
        style="<%= hidden unless !rubric || can_do(rubric_association, @current_user, :update) %>"
        title="<%= t 'links.find_another_rubric', "Find Another Rubric" %>"
        aria-label="<%= t 'links.find_another_rubric', "Find Another Rubric" %>"
        role="button"
      >{{> icons/search}}</a>
      <% if for_context %>
        <a
          href="<%= context_url(context, :context_rubric_url, rubric ? rubric.id : "{{ id }}") %>"
          class="delete_rubric_link no-print no-hover"
          style="<%= hidden unless !rubric || can_do(rubric_association, @current_user, :delete) %>"
          title="<%= t 'links.delete', "Delete Rubric" %>"
          aria-label="<%= t 'links.delete', "Delete Rubric" %>"
          role="button"
        >{{> icons/trash}}</a>
      <% else %>
        <a
          href="<%= context_url(context, :context_rubric_association_url, rubric_association ? rubric_association.id : "{{ rubric_association_id }}") %>"
          class="delete_rubric_link no-print no-hover"
          style="<%= hidden unless !rubric || can_do(rubric_association, @current_user, :delete) %>"
          title="<%= t 'links.delete', "Delete Rubric" %>"
          aria-label="<%= t 'links.delete', "Delete Rubric" %>"
          role="button"
        >{{> icons/trash}}</a>
      <% end %>
      <div style="display: none;">
        <div class="use_for_grading"><%= (rubric_association && rubric_association.use_for_grading) ? "true" : nbsp %></div>
        <div class="free_form_criterion_comments"><%= (rubric && rubric.free_form_criterion_comments) ? "true" : nbsp %></div>
        <div class="hide_score_total"><%= (rubric && rubric.hide_score_total) ? "true" : nbsp %></div>
        <div class="rubric_association_id"><%= rubric_association ? rubric_association.id : nbsp %></div>
        <div class="user_id"><%= assessment ? assessment.user_id : nbsp %></div>
        <div class="assessment_type"><%= assessment ? assessment.assessment_type : "" %></div>
        <a href="<%= context_url(context, :context_rubric_association_rubric_assessment_url, rubric_association ? rubric_association.id : "{{ rubric_association_id }}", (assessment ? assessment.id : "{{ assessment_id }}")) %>" rel="nofollow" class="edit_assessment_link">&nbsp;</a>
        <a href="<%= context_url(context, :context_rubric_url, "{{ rubric_id }}") %>" class="edit_rubric_url">&nbsp;</a>
        <% if for_context %>
          <a href="<%= context_url(context, :context_rubric_url, rubric ? rubric.id : "{{ id }}") %>" class="delete_rubric_url">&nbsp;</a>
        <% else %>
          <a href="<%= context_url(context, :context_rubric_association_url, "{{ association_id }}") %>" class="delete_rubric_url">&nbsp;</a>
        <% end %>
      </div>
    </div>
    <div style="float: right; font-size: 0.8em; display: none;" class="links displaying locked">
      <span style="<%= hidden if editable %>"><%= t 'messages.locked', "Can't change a rubric once you've started using it." %></span>
      <% if for_context %>
        <a href="<%= context_url(context, :context_rubric_url, rubric ? rubric.id : "{{ id }}") %>" class="delete_rubric_url" style="display: none;">&nbsp;</a>
      <% else %>
        <a href="<%= context_url(context, :context_rubric_association_url, "{{ association_id }}") %>" class="delete_rubric_url" style="display: none;">&nbsp;</a>
      <% end %>
    </div>

    <div class="editing" style="float: right;">
      <a href="<%= search_rubrics_url(:q => "") %>" class="find_rubric_link icon-search" style="<%= hidden unless !rubric || can_do(rubric_association, @current_user, :update) %>" title="<%= t 'titles.find_existing_rubric', "Find Existing Rubric" %>"><%= t 'links.find_a_rubric', "Find a Rubric" %></a>
    </div>
    <div class="editing" style="text-align: left">
      <label for="rubric-title">{{#t}}Title:{{/t}}</label>
      <input id="rubric-title" type="text" class="no-margin-bottom" name="title" value="<%= rubric.title rescue t('defaults.rubric_title', "Some Rubric") %>" style="width: 200px;" maxlength="255" aria-label="{{#t}}Title:{{/t}}"/>
      <a href="<%= search_rubrics_url(:q => "") %>" style="display: none;"><%= image_tag "find.png", :alt => '' %> <%= t 'links.find_rubric', "Find Rubric" %></a>
      <% if rubric_association && !rubric_association.rubric_assessments.empty? %>
        <div class="editing" style="font-size: 0.8em;"><%= t 'messages.rubric_in_use',
                                                             { :one => "Keep in mind that 1 student has already been assessed using this rubric.  Changing it will affect their evaluation.",
                                                               :other => "Keep in mind that %{count} students have already been assessed using this rubric.  Changing it will affect their evaluations." },
                                                           :count => rubric_association.rubric_assessments.count %></div>
      <% end %>
    </div>
    <div class="displaying">
      <span class="title" tabindex="-1"><%= rubric.try(:title) || t(:rubric_title, "Title") %></span>
    </div>
    <div class="has-assessments-warning" style="<%= hidden unless edit_view && has_assessments %>">
      <%= t 'messages.rubric_in_use2', "You've already rated students with this rubric.  Any major changes could affect their assessment results." %>
    </div>
  </div>
<table class="rubric_table">
<caption>
  <div class="screenreader-only">
    <span class="title"><%= rubric.try(:title) || t(:rubric_title, "Title") %></span>
  </div>
</caption>
<thead>
  <tr>
    <th scope="col"><%= t 'headers.criteria', "Criteria" %></th>
    <th scope="col"><%= t 'headers.ratings', "Ratings" %></th>
    <th scope="col"><%= t 'headers.points', "Pts" %></th>
  </tr>
</thead>
<tbody>
  <% if rubric && rubric.criteria %>
    <% rubric.criteria_object.each do |criterion| %>
      <% rating = assessment.ratings.find{|r| r[:criterion_id] == criterion.id} rescue nil %>
      {{> views/shared/rubric_criterion}}
    <% end %>
  <% end %>
  {{> views/shared/rubric_criterion}}
  <tr class="summary" style="<%= hidden if rubric_association && rubric_association.hide_score_total %>">
    <td colspan="4">
      <div style="float: right;" class="total_points_holder">
        <span><%= before_label :total_points, "Total Points" %>
          <% ot(:out_of, "%{points} *out of %{total}*", :points => capture { %>
            <span class="rubric_total">
              <%=
                assessment ? n(assessment.score) : n((rubric.points_possible rescue 5))
              %>
            </span>
            <%
              },
              wrapper: '<span class="assessing">\1</span>',
              total: n((rubric.points_possible rescue 5)))
            %>
        </span>
      </div>
      <div style="float: left;" class="editing">
        <span id="add_criterion_holder" class="criterion_link"></span>
      </div>
      <div class="clear"></div>
    </td>
  </tr>
</tbody>
</table>
</div>
<table style="display: none;">
  <tr id="edit_rubric">
    <td colspan="4">
      <form id="edit_rubric_form" class="edit-rubric-form no-margin-bottom">
        <div class="rubric_custom_ratings" style="<%= hidden unless @assignment %>">
          <input type="checkbox" id="rubric_custom_rating" class="rubric_custom_rating" <%= 'checked' if rubric && rubric.free_form_criterion_comments %>/>
          <label for="rubric_custom_rating"><%= t 'labels.custom_rating', "I'll write free-form comments when assessing students" %></label>
        </div>
        <% if show_grading %>
          <div class="rubric_grading" style="<%= hidden unless @assignment %>">
            <input type="checkbox" id="grading_rubric" class="grading_rubric_checkbox" <%= 'checked' if rubric_association && rubric_association.use_for_grading %>/>
            <label for="grading_rubric"><%= t 'labels.grading_rubric', "Use this rubric for assignment grading" %></label>
          </div>
        <% end %>
        <div class="totalling_rubric" style="<%= hidden unless @assignment %>">
          <input type="checkbox" id="totalling_rubric" class="totalling_rubric_checkbox" <%= 'checked' if rubric_association && rubric_association.hide_score_total %>/>
          <label for="totalling_rubric"><%= t 'labels.totallying_rubric', "Hide score total for assessment results" %></label>
        </div>
        <div class="ic-Action-header ic-Action-header--half-margin">
          <div class="ic-Action-header__Primary">
            <button type="button" class="Button cancel_button"><%= t '#buttons.cancel', "Cancel" %></button>
            <button type="submit" class="Button Button--primary save_button"><%= t 'buttons.create', "Create Rubric" %></button>
          </div>
        </div>
      </form>
    </td>
  </tr>
</table>
<% end %>
